/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import com.zimbra.cs.store.Blob;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.activation.DataSource;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class StoreIncomingBlob
/*     */   extends RedoableOp
/*     */ {
/*     */   static final int MAX_BLOB_SIZE = 104857600;
/*     */   static final int MAX_MAILBOX_LIST_LENGTH = 1000000;
/*     */   private String mDigest;
/*     */   private String mPath;
/*     */   private int mMsgSize;
/*     */   private RedoableOpData mData;
/*     */   private List<Integer> mMailboxIdList;
/*     */   
/*     */   public StoreIncomingBlob()
/*     */   {
/*  54 */     super(MailboxOperation.StoreIncomingBlob);
/*     */   }
/*     */   
/*     */   public StoreIncomingBlob(String digest, int msgSize, List<Integer> mboxIdList) {
/*  58 */     this();
/*  59 */     setMailboxId(-1);
/*  60 */     this.mDigest = (digest != null ? digest : "");
/*  61 */     this.mMsgSize = msgSize;
/*  62 */     this.mMailboxIdList = mboxIdList;
/*     */   }
/*     */   
/*     */   public List<Integer> getMailboxIdList() {
/*  66 */     return this.mMailboxIdList;
/*     */   }
/*     */   
/*     */   public void setMailboxIdList(List<Integer> list) {
/*  70 */     this.mMailboxIdList = list;
/*     */   }
/*     */   
/*     */   public void setBlobBodyInfo(File file) {
/*  74 */     this.mData = new RedoableOpData(file);
/*  75 */     this.mPath = file.getPath();
/*     */   }
/*     */   
/*     */   public void setBlobBodyInfo(DataSource ds, int dataLength, String path) {
/*  79 */     this.mData = new RedoableOpData(ds, dataLength);
/*  80 */     this.mPath = path;
/*     */   }
/*     */   
/*     */   protected String getPrintableData() {
/*  84 */     StringBuilder sb = new StringBuilder("blobDigest=\"");
/*  85 */     sb.append(this.mDigest).append("\", size=").append(this.mMsgSize);
/*  86 */     sb.append(", dataLen=").append(this.mData.getLength());
/*  87 */     sb.append(", path=").append(this.mPath);
/*  88 */     sb.append(", mbox=").append(this.mMailboxIdList == null ? "[]" : this.mMailboxIdList);
/*  89 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public InputStream getAdditionalDataStream() throws IOException {
/*  93 */     return this.mData.getInputStream();
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException {
/*  97 */     if (getVersion().atLeast(1, 0)) {
/*  98 */       if (this.mMailboxIdList != null) {
/*  99 */         out.writeInt(this.mMailboxIdList.size());
/* 100 */         for (Integer mboxId : this.mMailboxIdList)
/*     */         {
/* 102 */           if (getVersion().atLeast(1, 26)) {
/* 103 */             out.writeLong(mboxId.longValue());
/*     */           } else {
/* 105 */             out.writeInt(mboxId.intValue());
/*     */           }
/*     */         }
/*     */       } else {
/* 109 */         out.writeInt(0);
/*     */       }
/*     */     }
/* 112 */     out.writeUTF(this.mDigest);
/* 113 */     out.writeUTF(this.mPath);
/* 114 */     out.writeShort((short)-1);
/* 115 */     out.writeInt(this.mMsgSize);
/* 116 */     out.writeInt(this.mData.getLength());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void deserializeData(RedoLogInput in)
/*     */     throws IOException
/*     */   {
/* 127 */     if (getVersion().atLeast(1, 0)) {
/* 128 */       int listLen = in.readInt();
/* 129 */       if (listLen > 1000000) {
/* 130 */         throw new IOException("Deserialized mailbox list too large (" + listLen + ")");
/*     */       }
/* 132 */       if (listLen >= 1) {
/* 133 */         List<Integer> list = new ArrayList(listLen);
/* 134 */         for (int i = 0; i < listLen; i++)
/*     */         {
/* 136 */           if (getVersion().atLeast(1, 26)) {
/* 137 */             list.add(Integer.valueOf((int)in.readLong()));
/*     */           } else {
/* 139 */             list.add(Integer.valueOf(in.readInt()));
/*     */           }
/*     */         }
/* 142 */         this.mMailboxIdList = list;
/*     */       }
/*     */     }
/* 145 */     this.mDigest = in.readUTF();
/* 146 */     this.mPath = in.readUTF();
/* 147 */     in.readShort();
/* 148 */     this.mMsgSize = in.readInt();
/* 149 */     int dataLen = in.readInt();
/*     */     
/*     */ 
/*     */ 
/* 153 */     long pos = in.getFilePointer();
/* 154 */     this.mData = new RedoableOpData(new File(in.getPath()), pos, dataLen);
/*     */     
/*     */ 
/* 157 */     int numSkipped = in.skipBytes(dataLen);
/* 158 */     if (numSkipped != dataLen) {
/* 159 */       String msg = String.format("Attempted to skip %d bytes at position %d in %s, but actually skipped %d.", new Object[] { Integer.valueOf(dataLen), Long.valueOf(pos), in.getPath(), Integer.valueOf(numSkipped) });
/*     */       
/* 161 */       throw new IOException(msg);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void redo()
/*     */     throws Exception
/*     */   {
/* 170 */     StoreIncomingBlob redoRecorder = null;
/* 171 */     if (!getUnloggedReplay()) {
/* 172 */       redoRecorder = new StoreIncomingBlob(this.mDigest, this.mMsgSize, this.mMailboxIdList);
/* 173 */       redoRecorder.start(getTimestamp());
/* 174 */       redoRecorder.setBlobBodyInfo(new RedoableOpDataSource(this.mData), this.mData.getLength(), this.mPath);
/* 175 */       redoRecorder.log();
/*     */     }
/*     */     
/* 178 */     boolean success = false;
/*     */     try {
/* 180 */       boolean compressed = this.mData.getLength() != this.mMsgSize;
/* 181 */       Blob blob = StoreManager.getInstance().storeIncoming(this.mData.getInputStream(), compressed);
/* 182 */       if (compressed) {
/* 183 */         blob.setDigest(this.mDigest).setRawSize(this.mMsgSize).setCompressed(compressed);
/*     */       }
/* 185 */       registerBlob(this.mPath, blob);
/* 186 */       success = true;
/*     */     } finally {
/* 188 */       if (redoRecorder != null) {
/* 189 */         if (success) {
/* 190 */           redoRecorder.commit();
/*     */         } else {
/* 192 */           redoRecorder.abort();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 198 */   private static final Map<String, Blob> sReplayedBlobs = new HashMap();
/*     */   
/* 200 */   static void registerBlob(String path, Blob blob) { synchronized (sReplayedBlobs) {
/* 201 */       sReplayedBlobs.put(path, blob);
/*     */     }
/*     */   }
/*     */   
/*     */   /* Error */
/*     */   static Blob fetchBlob(String path)
/*     */   {
/*     */     // Byte code:
/*     */     //   0: getstatic 83	com/zimbra/cs/redolog/op/StoreIncomingBlob:sReplayedBlobs	Ljava/util/Map;
/*     */     //   3: dup
/*     */     //   4: astore_1
/*     */     //   5: monitorenter
/*     */     //   6: getstatic 83	com/zimbra/cs/redolog/op/StoreIncomingBlob:sReplayedBlobs	Ljava/util/Map;
/*     */     //   9: aload_0
/*     */     //   10: invokeinterface 85 2 0
/*     */     //   15: checkcast 86	com/zimbra/cs/store/Blob
/*     */     //   18: aload_1
/*     */     //   19: monitorexit
/*     */     //   20: areturn
/*     */     //   21: astore_2
/*     */     //   22: aload_1
/*     */     //   23: monitorexit
/*     */     //   24: aload_2
/*     */     //   25: athrow
/*     */     // Line number table:
/*     */     //   Java source line #205	-> byte code offset #0
/*     */     //   Java source line #206	-> byte code offset #6
/*     */     //   Java source line #207	-> byte code offset #21
/*     */     // Local variable table:
/*     */     //   start	length	slot	name	signature
/*     */     //   0	26	0	path	String
/*     */     //   4	19	1	Ljava/lang/Object;	Object
/*     */     //   21	4	2	localObject1	Object
/*     */     // Exception table:
/*     */     //   from	to	target	type
/*     */     //   6	20	21	finally
/*     */     //   21	24	21	finally
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/StoreIncomingBlob.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */